#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "navbtn.h"
#include "handtopbtn.h"
#include "parameterbtn.h"
#include <QMap>
#include <QTimer>
#include <QSerialPort>
#include <QMessageBox>
#include <QFile>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QSslConfiguration>

#include <QStandardPaths>
#include <QDir>
#include <QFileInfo>
#include <QUuid>
#include <QEventLoop>

#include "monitorwidget.h"
#include "handwidget.h"
#include "paramterwidget.h"
#include "aipredictionwidget.h"
#include "oasis.h"
#include "comm.h"
#include "tabel.h"
#include "weatherwidget.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void init();

    void get_init();

    //初始化监控界面
    void initMonitorForm();

    //初始化手动界面
    void initHandForm();

    //初始化参数界面
    void initParamterForm();

    void V4L2_init();

    void db_init();  //数据库初始化函数

    bool db_update();

    bool db_select();

    quint16 calcCRC(const QByteArray &data);

    /*******HTTP连接华为云接口******/
    void MQTT_Cmd_Send(QString serviceId);

    void getDeviceProperties(std::function<void(const QJsonObject&, int)> callback);

    void onRefreshPropertiesClicked();

    void sendMessageToDevice(const QJsonObject& contentObj);

    void saveTokenToFile(const QByteArray& token);

    QByteArray loadTokenFromFile();

    bool eventFilter(QObject *watched, QEvent *event);


private slots:
   void get_token(); //获取token
   void fan_on();//开风扇
   void fan_off();//关风扇
   void led_on();//开灯
   void led_off();//关灯
   void buzzer_on();//开蜂鸣器
   void buzzer_off();//关蜂鸣器
   bool resetTimeTable(); //复位

   /*******HTTP连接华为云槽******/
   void replyFinished(QNetworkReply *reply);

private:
    Ui::MainWindow *ui;

    struct data dd;

    oasis *login_o;

    Tabel *my_table;

    WeatherWidget *my_weather;//天气界面

    MonitorWidget* m_MonitorWidget;//监控界面

    HandWidget* m_HandWidget;//手动界面
    ParamterWidget* m_ParamterWidget;//参数界面

    AIPredictionWidget* m_AIPredictionWidget; //AI预测界面

    QMap<QString,NavBtn*> m_NavBtnMap;//导航栏按钮
    QTimer m_timer;
    QTimer m_timer_2;

    bool messageBox;     

    float tm_time = 0;
    int hm_time = 0;
    int ill_time = 0;

    /*******HTTP连接华为云参数******/
    int function_select = 0;

    QByteArray Token = {0};

    QNetworkAccessManager *manger;

    QString TOKEN_FILE;

    // 添加回调函数成员变量
    std::function<void(const QJsonObject&, int)> currentCallback;

    //主账号用户名
    QString MAIN_USER = "hw091214826";
    //IAM子用户名
    QString IAM_USER = "ds_abc";
    //IAM子密码
    QString IAM_PASSWORD = "DS12345678";
    //项目ID，在我的凭证中
    QString PROJECT_ID = "8cf7b43a2a43485da2a4a92c46f955ef";
    //服务器区域代号
    QString SERVER_ID = "cn-north-4";
    //物联网产品的服务ID
    QString service_id = "stm32";
    //物联网产品的设备ID
    QString device_id ="686cd7c2d582f200183946de_dev1";
    //域名
    QString ID_ADDR = "489fb75c06.st1.iotda-app.cn-north-4.myhuaweicloud.com";
};
#endif // MAINWINDOW_H
